package form.LogForm;

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JComboBox;
import javax.swing.JTable;

import customGUI.selectors.AssetSelector;


import db.Asset;

import form.Form;
import form.AssetForm.AssetForm;

public class LogForm extends Form {

	private JComboBox<String> selector;
	private JTable table;
	private AssetForm viewForm;
	
	public void setSelector(JComboBox<String> selector){
		this.selector = selector;
	}
	
	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public AssetForm getViewForm() {
		return viewForm;
	}

	public void setViewForm(AssetForm viewForm) {
		this.viewForm = viewForm;
	}
	
	public void refreshComboBox(){
		
		if(selector != null && table!=null){
			ArrayList<String> assetNames = new ArrayList<String>();
			
			selector.removeAllItems();
			selector.addItem("All");
			String currString;
			int rowCount = table.getRowCount() - 1;	
			
			ArrayList<String> ids = new ArrayList<String>();
			
			for(int i=0;i<rowCount;i++){
				currString = table.getModel().getValueAt(i, 1).toString();
				if(!ids.contains(currString))
					ids.add(currString);
			}
				
			for(int i=0;i<ids.size();i++)
				assetNames.add(Asset.retrieveAssetName(Integer.parseInt(ids.get(i)))+"-"+ids.get(i));
			
				
			Collections.sort(assetNames);
			
			for(int i = 0; i < assetNames.size(); i++)
				selector.addItem(assetNames.get(i));
		}
	}

	@Override
	public void clear() {
		if(viewForm != null)
			viewForm.clear();
	}

	@Override
	public void refresh() {
		if(selector != null)
		{
			refreshComboBox();
			clear();
		}
	}

}
